#version 330 core
layout (triangles) in;
layout (triangle_strip, max_vertices = 3) out;
vec4 adjustedPositions[3];
uniform mat4 transform;
void main() {
    for(int i = 0; i < 3; ++i)
    {
        adjustedPositions[i] = transform * gl_in[i].gl_Position;
    }
    if (adjustedPositions[0].x < -1.0)
    {
        float dis = -1.0 - gl_in[0].gl_Position.x;
        adjustedPositions[0].x = -1.0;
        adjustedPositions[1].x = gl_in[1].gl_Position.x + dis;
        adjustedPositions[2].x = gl_in[2].gl_Position.x + dis;

    }
    if (adjustedPositions[1].x > 1.0)
    {
        float dis = 1.0 - gl_in[1].gl_Position.x;
        adjustedPositions[1].x = 1.0;
        adjustedPositions[0].x = gl_in[0].gl_Position.x + dis;
        adjustedPositions[2].x = gl_in[2].gl_Position.x + dis;
    }
    if (adjustedPositions[1].y < -1.0)
    {
        float dis = -1.0 - gl_in[1].gl_Position.y;
        adjustedPositions[1].y = -1.0;
        adjustedPositions[0].y = gl_in[0].gl_Position.y + dis;
        adjustedPositions[2].y = gl_in[2].gl_Position.y + dis;
    }
    if (adjustedPositions[2].y > 1.0)
    {
        float dis = 1.0 - gl_in[2].gl_Position.y;
        adjustedPositions[2].y = 1.0;
        adjustedPositions[0].y = gl_in[0].gl_Position.y + dis;
        adjustedPositions[1].y = gl_in[1].gl_Position.y + dis;
    }
    for(int i = 0; i < 3; ++i)
    {
        gl_Position = adjustedPositions[i] ;
        EmitVertex();
    }
    
    EndPrimitive();
}